package com.nacrtstudio.webgiftshop.pages;

import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.nacrtstudio.webgiftshop.facade.Controller;
import com.nacrtstudio.webgiftshop.model.shop.User;

public class ChangePassword {
	@Inject
	private Controller controller;
	@Property
	private User user;
	@Property
	private String password;
	@Property
	private String newPassword;
	@Property
	private String newRePassword;
	
	@InjectComponent
	private Form form;
	@InjectPage
	private StatusPage statusPage;
	
	void onActivate() {
		try {
			if (controller.isLoggedIn()) {
				user = controller.getLoggedUser();
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public Object onSubmitFromForm(){
		try {
			if (user.getPassword().equals(password)) {
				if (newPassword.equals(newRePassword)) {
					user.setPassword(newPassword);
					controller.updateUser(user);
					statusPage.setMessage("Uspesno ste promeni Vas password");
					return statusPage;
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		statusPage.setMessage("Greska, pokusajte ponovo!");
		return statusPage;
	}
	
}
